package com.example.mvvmtest.views;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.mvvmtest.R;
import com.example.mvvmtest.test.Test3;
import com.example.mvvmtest.utils.LogUtil;

public class Fragment1 extends Fragment {
    private String title;
    private int bgColor;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtil.e("Fragment1-onCreateView");
        view = inflater.inflate(R.layout.fragment_1, container, false);
        initView(view);
        initEvent(view);
        return view;
    }

    private void initEvent(View view) {
        view.findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.e("Fragment1 - btn1");
            }
        });
        view.findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.e("Fragment1 - btn2");
            }
        });
    }

    private void initData() {
        Bundle bundle = getArguments() != null ? getArguments() : new Bundle();
        title = bundle.getString("title", "");
        bgColor = bundle.getInt("bg", 0);
        LogUtil.e("初始化数据：" + "title = " + title + ",bgColor = " + bgColor);

//        final String urlPath = "/zmgame/update/1.3/65217870458162921.apk";
//        final String md5Str = "2F9D747C24B7FAD224FBC782BBB27201";
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                boolean result = CheckResUtil.getInstance().checkFile(urlPath,md5Str);
//                LogUtil.e("校验结果：" + result);
//            }
//        },0);

    }

    private void initView(View view) {
        TextView tv2 = view.findViewById(R.id.tv_2);
        tv2.setText(title);
        if (bgColor != 0) {
            //ConstraintLayout constraintLayout = view.findViewById(R.id.rootView);
            tv2.setBackgroundColor(getResources().getColor(bgColor));
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtil.e("Fragment1-onAttach");
        initData();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.e("Fragment1-onCreate");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtil.e("Fragment1-onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.e("Fragment1-onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.e("Fragment1-onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.e("Fragment1-onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.e("Fragment1-onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtil.e("Fragment1-onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.e("Fragment1-onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtil.e("Fragment1-onDetach");
    }
}
